home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / exec / obtainsemaphore.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  3KB  |  106 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: obtainsemaphore.c,v 1.4 1996/08/13 13:56:04 digulla Exp $
  4.     $Log: obtainsemaphore.c,v $
  5.     Revision 1.4  1996/08/13 13:56:04  digulla
  6.     Replaced __AROS_LA by __AROS_LHA
  7.     Replaced some __AROS_LH*I by __AROS_LH*
  8.     Sorted and added includes
  9.  
  10.     Revision 1.3  1996/08/01 17:41:14  digulla
  11.     Added standard header for all files
  12.  
  13.     Desc:
  14.     Lang: english
  15. */
  16. #include "exec_intern.h"
  17. #include "semaphores.h"
  18.  
  19. /*****************************************************************************
  20.  
  21.     NAME */
  22.     #include <exec/semaphores.h>
  23.     #include <clib/exec_protos.h>
  24.  
  25.     __AROS_LH1(void, ObtainSemaphore,
  26.  
  27. /*  SYNOPSIS */
  28.     __AROS_LHA(struct SignalSemaphore *, sigSem, A0),
  29.  
  30. /*  LOCATION */
  31.     struct ExecBase *, SysBase, 94, Exec)
  32.  
  33. /*  FUNCTION
  34.     Obtain an exclusive lock on a semaphore. If the semaphore is already
  35.     in use by another task this function will wait until the semaphore
  36.     becomes free.
  37.  
  38.     INPUTS
  39.     sigSem - Pointer to semaphore structure
  40.  
  41.     RESULT
  42.  
  43.     NOTES
  44.     This function preserves all registers.
  45.  
  46.     EXAMPLE
  47.  
  48.     BUGS
  49.  
  50.     SEE ALSO
  51.     ReleaseSemaphore()
  52.  
  53.     INTERNALS
  54.  
  55.     HISTORY
  56.     29-10-95    digulla automatically created from
  57.                 exec_lib.fd and clib/exec_protos.h
  58.     21-01-96    fleischer implementation
  59.  
  60. *****************************************************************************/
  61. {
  62.     __AROS_FUNC_INIT
  63.     __AROS_BASE_EXT_DECL(struct ExecBase *,SysBase)
  64.     struct Task *me;
  65.  
  66.     /* Get pointer to current task */
  67.     me=SysBase->ThisTask;
  68.  
  69.     /* Arbitrate for the semaphore structure */
  70.     Forbid();
  71.  
  72.     /* Check if the semaphore is in use by another task */
  73.     if(sigSem->ss_NestCount&&sigSem->ss_Owner!=me)
  74.     {
  75.     /*
  76.         I wish there was some shared memory available as a semaphore node.
  77.         Unfortunately it isn't - and I cannot rely on being able to get
  78.         some from AllocMem() at this point either, so I have to use a part
  79.         of the stack instead :-(.
  80.     */
  81.     struct SemaphoreNode sn;
  82.  
  83.     sn.node.ln_Pri =SN_TYPE_OBTAIN;
  84.     sn.node.ln_Name=(char *)SM_EXCLUSIVE;
  85.     sn.task        =me;
  86.  
  87.     /* Add the node to the semaphore's waiting queue. */
  88.     AddTail((struct List *)&sigSem->ss_WaitQueue,&sn.node);
  89.  
  90.     /* Wait until the semaphore is free */
  91.     Wait(SEMAPHORESIGF);
  92.  
  93.     /* ss_NestCount and ss_Owner are already set by ReleaseSemaphore() */
  94.     }else
  95.     {
  96.     /* The semaphore is free - take it over */
  97.     sigSem->ss_NestCount++;
  98.     sigSem->ss_Owner=me;
  99.     }
  100.  
  101.     /* All done */
  102.     Permit();
  103.     __AROS_FUNC_EXIT
  104. } /* ObtainSemaphore */
  105.  
  106.